package cn.bytemecc.bloomFilter.service.judge.impl;

import cn.bytemecc.bloomFilter.bitmap.DefaultBitmap;
import cn.bytemecc.bloomFilter.cache.SimpleCaffeine;
import cn.bytemecc.bloomFilter.service.judge.IBloomFilterService;
import cn.bytemecc.bloomFilter.strategy.IHashGenerator;

/**
 * @description: TODO
 * @author: Liang Zi cao
 * @DATE: 2023/7/16
 * Talk is cheap. Show me the code
 **/

public class BloomFilterServiceImpl implements IBloomFilterService {
    private IHashGenerator hashGenerator;
    private SimpleCaffeine simpleCaffeine;

    public BloomFilterServiceImpl(IHashGenerator hashGenerator, SimpleCaffeine simpleCaffeine) {
        this.hashGenerator = hashGenerator;
        this.simpleCaffeine = simpleCaffeine;
    }

    @Override
    public boolean isHave(String bitMapKey,Object value) {
        DefaultBitmap defaultBitmap = simpleCaffeine.getBitMap(bitMapKey);
        if (defaultBitmap==null){
            return false;
        }
        Integer hash = hashGenerator.doHash(value, defaultBitmap.getSize());
        return defaultBitmap.getBit(hash);
    }
}
